Skip to content

refactor: extract type-check boilerplate into a single require_internal_slot! macro - #5233

Merged
jedel1043 merged 12 commits into
boa-dev:mainfrom
Nakshatra480:refactor/temporal-this-type-check
Nov 24, 2026
Merged

jedel1043 merged 12 commits into
boa-dev:mainfrom
Nakshatra480:refactor/temporal-this-type-check

Conversation

@Nakshatra480

@Nakshatra480 Nakshatra480 commented Mar 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR introduces a new require_internal_slot! macro to replace the repetitive 5-line this-type-checking boilerplate found across the engine's built-in prototype methods.

Previously, almost every prototype method (across Temporal, Date, TypedArrays, etc.) manually downcasted this and returned a TypeError if the internal slot was missing:

let object = this.as_object();
let date = object
    .as_ref()
    .and_then(JsObject::downcast_ref::<Self>)
    .ok_or_else(|| JsNativeError::typ().with_message("'this' is not a Date"))?;

This PR refactors those occurrences into a single, much cleaner macro call that correctly handles the necessary borrowing and lifetime scoping:

require_internal_slot!(date = this, Date, "Date");

Scope of changes

While originally intended for just the Temporal built-ins, I realized this boilerplate is heavily duplicated across the entire engine. Therefore, I've broadened the scope and applied the macro to all affected built-ins, including:

  • Temporal (all modules)
  • Date
  • ArrayBuffer & SharedArrayBuffer
  • DataView
  • TypedArray
  • WeakRef, WeakMap, WeakSet
  • Intl (Collator, Segmenter, Segments)

Error Messages & Testing

As part of standardizing this pattern, all error messages across the engine for missing internal slots have been unified to use the generic format: TypeError: "the this object must be a {Type} object.".

All existing tests (specifically in Builtin::Date) that previously relied on custom error strings like "'this' is not a Date" have been updated to expect the new standardized message. CI tests now pass cleanly with these updates.

@github-actions github-actions Bot added Waiting On Review Waiting on reviews from the maintainers C-Builtins PRs and Issues related to builtins/intrinsics and removed Waiting On Review Waiting on reviews from the maintainers labels Mar 23, 2026
@github-actions github-actions Bot added this to the v1.0.0 milestone Mar 23, 2026
@Nakshatra480
Nakshatra480 force-pushed the refactor/temporal-this-type-check branch from 4053092 to 4c42492 Compare March 23, 2026 05:48
@github-actions github-actions Bot added the Waiting On Review Waiting on reviews from the maintainers label Mar 23, 2026
@github-actions

github-actions Bot commented Mar 23, 2026 •

Copy link
Copy Markdown

Test262 conformance changes

Test result main count PR count difference
Total 53,578 53,578 0
Passed 51,464 51,457 -7
Ignored 1,629 1,629 0
Failed 485 492 +7
Panics 0 0 0
Conformance 96.05% 96.04% -0.01%
Broken tests (7):
test/built-ins/Map/groupBy/negativeZero.js (previously Passed)
test/built-ins/Map/groupBy/groupLength.js (previously Passed)
test/built-ins/Map/groupBy/string.js (previously Passed)
test/built-ins/Map/groupBy/toPropertyKey.js (previously Passed)
test/built-ins/Map/groupBy/emptyList.js (previously Passed)
test/built-ins/Map/groupBy/evenOdd.js (previously Passed)
test/staging/sm/Array/group.js (previously Passed)

Tested main commit: 6801ee246df7bb801434c0dd65138b8f95e0065b
Tested PR commit: ad612b8be7c9def240cd3ae7d899e504f9814739
Compare commits: 6801ee2...ad612b8

@codecov

codecov Bot commented Mar 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 33.99766% with 563 lines in your changes missing coverage. Please review.
✅ Project coverage is 63.05%. Comparing base (6ddc2b4) to head (ad612b8).
⚠️ Report is 1089 commits behind head on main.

Files with missing lines Patch % Lines
.../engine/src/builtins/temporal/zoneddatetime/mod.rs 0.00% 116 Missing ⚠️
...ngine/src/builtins/temporal/plain_date_time/mod.rs 2.00% 98 Missing ⚠️
...gine/src/builtins/temporal/plain_year_month/mod.rs 0.00% 58 Missing ⚠️
core/engine/src/builtins/temporal/duration/mod.rs 14.81% 46 Missing ⚠️
...ore/engine/src/builtins/temporal/plain_date/mod.rs 0.00% 46 Missing ⚠️
core/engine/src/builtins/temporal/instant/mod.rs 0.00% 45 Missing ⚠️
...ore/engine/src/builtins/temporal/plain_time/mod.rs 10.00% 36 Missing ⚠️
...ngine/src/builtins/temporal/plain_month_day/mod.rs 0.00% 28 Missing ⚠️
core/engine/src/builtins/array_buffer/mod.rs 13.33% 13 Missing ⚠️
.../src/builtins/iterable/async_from_sync_iterator.rs 0.00% 9 Missing ⚠️
... and 30 more
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5233       +/-   ##
===========================================
+ Coverage   47.24%   63.05%   +15.81%     
===========================================
  Files         476      538       +62     
  Lines       46892    58581    +11689     
===========================================
+ Hits        22154    36939    +14785     
+ Misses      24738    21642     -3096     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nekevss nekevss left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do sort of like the change for how succinct it is, and it's potential for making the code more readable.

One question: why change just the temporal objects and not the built-ins in general? Had you looked at other built-ins?

@Nakshatra480
Nakshatra480 force-pushed the refactor/temporal-this-type-check branch from 4c42492 to 56ac6f3 Compare March 25, 2026 09:38
@Nakshatra480
Nakshatra480 requested a review from nekevss March 25, 2026 10:50
@Nakshatra480
Nakshatra480 marked this pull request as draft March 25, 2026 14:57
@Nakshatra480
Nakshatra480 force-pushed the refactor/temporal-this-type-check branch from 56ac6f3 to 4fee1fe Compare March 25, 2026 15:02
@github-actions github-actions Bot added the C-Intl Changes related to the `Intl` implementation label Mar 25, 2026
@Nakshatra480
Nakshatra480 marked this pull request as ready for review March 25, 2026 15:05
@Nakshatra480
Nakshatra480 requested a review from a team as a code owner March 25, 2026 15:05
@Nakshatra480
Nakshatra480 marked this pull request as draft March 25, 2026 15:16
@Nakshatra480
Nakshatra480 force-pushed the refactor/temporal-this-type-check branch from 4fee1fe to 2fdc570 Compare March 25, 2026 16:37
@github-actions github-actions Bot added the C-Tests Issues and PRs related to the tests. label Mar 25, 2026
@Nakshatra480
Nakshatra480 force-pushed the refactor/temporal-this-type-check branch from 2fdc570 to 4c4edd1 Compare March 25, 2026 17:20
…lot! macro

Replace the repeated as_object/downcast_ref/ok_or_else boilerplate with
a single require_internal_slot! macro defined in builtins/mod.rs.

The macro produces two let-bindings in the caller's scope: one for the
owned JsObject (to keep it alive) and one for the downcast Ref<T>.
Uses the js_error! macro internally for error construction.

Temporal modules (163 replacements):
- zoneddatetime, plain_date_time, plain_time, plain_year_month,
  plain_date, duration, instant, plain_month_day

Other builtins (32 replacements):
- date (9), dataview (5), typed_array (4), array_buffer (4),
  shared_array_buffer (3), weak_map (2), intl/segmenter (2),
  intl/collator (1), weak_set (1), weak_ref (1)
@Nakshatra480
Nakshatra480 force-pushed the refactor/temporal-this-type-check branch from 4c4edd1 to fb5096a Compare March 25, 2026 17:23
@Nakshatra480 Nakshatra480 changed the title refactor(temporal): extract this-type-check boilerplate into a macro refactor: extract [this](cci:1://file:///Users/nakshatrasharma/Desktop/boa/core/engine/src/builtins/date/tests.rs:100:0-107:1) type-check boilerplate into a single require_internal_slot! macro Mar 25, 2026
@Nakshatra480 Nakshatra480 changed the title refactor: extract [this](cci:1://file:///Users/nakshatrasharma/Desktop/boa/core/engine/src/builtins/date/tests.rs:100:0-107:1) type-check boilerplate into a single require_internal_slot! macro refactor: extract type-check boilerplate into a single require_internal_slot! macro Mar 25, 2026
@Nakshatra480
Nakshatra480 marked this pull request as ready for review March 25, 2026 18:02
@Nakshatra480

Copy link
Copy Markdown
Contributor Author

Hey @nekevss Thanks for the review!

To answer your question: Yes, while originally working on Temporal, I realized that the 5-line this-type-check boilerplate is heavily duplicated across almost all built-ins. I have now expanded the scope of this PR to apply the macro across the entire engine, not just Temporal.

Here is a summary of the updates in the latest push:

  1. Scope Expanded: Applied the macro to all affected built-ins, including Date, ArrayBuffer, SharedArrayBuffer, DataView, TypedArray, WeakRef, WeakMap, WeakSet, and Intl (Collator, Segmenter, Segments), replacing hundreds of lines of repetitive downcasting boilerplate.
  2. Macro Renamed: Renamed the macro to require_internal_slot! as suggested.
  3. js_error! Used: Updated the macro to use js_error! internally as requested.
  4. Error Messages: The macro standardizes the error message to TypeError: "the this object must be a {Type} object.". A few tests (specifically in Builtin::Date) that explicitly asserted the old "'this' is not a Date" error string have been updated to expect the new standardized message.

All CI tests, including cargo fmt and cargo clippy, now pass cleanly. Let me know if there's anything else you'd like me to adjust!

Comment thread core/engine/src/builtins/mod.rs Resolved
Switch the require_internal_slot! macro to use JsObject::downcast instead
of downcast_ref, returning JsObject<T> (owned handle) rather than Ref<T>.
This prevents GcRefCell borrow panics by ensuring borrows are only held
for the duration of specific read/write operations.

Updated all call sites across 20 built-in modules to use
.borrow().data() / .borrow_mut().data_mut() for inner field access:
- Date (renamed macro binding to date_obj in setters to avoid shadowing)
- Temporal (Duration, Instant, PlainDate, PlainDateTime, PlainTime,
  PlainMonthDay, PlainYearMonth, ZonedDateTime)
- ArrayBuffer, SharedArrayBuffer, DataView, TypedArray
- WeakRef, WeakMap, WeakSet
- Intl (Collator, Segmenter, Segments)
@Nakshatra480
Nakshatra480 marked this pull request as draft March 25, 2026 19:52
@Nakshatra480
Nakshatra480 marked this pull request as ready for review March 25, 2026 20:09
@Nakshatra480
Nakshatra480 requested a review from jedel1043 March 25, 2026 20:09
@Nakshatra480

Nakshatra480 commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Hey @jedel1043 @nekevss I've pushed the requested changes using JsObject::downcast, and all builds and tests are now passing cleanly. The PR is ready for another look whenever you have time!

@Nakshatra480
Nakshatra480 marked this pull request as draft September 2, 2026 10:30
@github-actions github-actions Bot added C-Dependencies Pull requests that update a dependency file C-CLI Issues and PRs related to the Boa command line interface. C-FFI Foreign Function Interface, to be able to use Boa from a different language, such as C C-Runtime Issues and PRs related to Boa's runtime features labels Nov 22, 2026
@jedel1043
jedel1043 marked this pull request as ready for review November 22, 2026 20:44

@jedel1043 jedel1043 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took the liberty of doing cleanup to merge the latest changes + switch to a method instead of a macro, but aside from that looks great!

@jedel1043
jedel1043 enabled auto-merge November 22, 2026 20:45
@Nakshatra480

Copy link
Copy Markdown
Contributor Author

Took the liberty of doing cleanup to merge the latest changes + switch to a method instead of a macro, but aside from that looks great!

@jedel1043 Thanks for Approving this PR 🥳

@jedel1043
jedel1043 added this pull request to the merge queue Nov 24, 2026
Merged via the queue into boa-dev:main with commit 2d8a534 Nov 24, 2026
25 checks passed
@github-actions github-actions Bot removed the Waiting On Review Waiting on reviews from the maintainers label Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Builtins PRs and Issues related to builtins/intrinsics C-CLI Issues and PRs related to the Boa command line interface. C-Dependencies Pull requests that update a dependency file C-FFI Foreign Function Interface, to be able to use Boa from a different language, such as C C-Intl Changes related to the `Intl` implementation C-Runtime Issues and PRs related to Boa's runtime features C-Tests Issues and PRs related to the tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants